package edu.zut.software.identity.Service.impl;

import edu.zut.software.identity.Repository.StuInfoRepository;
import edu.zut.software.identity.Repository.StudentRepository;
import edu.zut.software.identity.Service.StudentService;
import edu.zut.software.identity.domain.StuCollege;
import org.springframework.data.jpa.domain.Specification;
import edu.zut.software.identity.domain.StuInfo;
import edu.zut.software.identity.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    public StudentRepository studentRepository;
    @Autowired
    public void setStudentRepository(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }
    public StuInfoRepository stuInfoRepository;
    @Autowired
    public void setStuInfoRepository(StuInfoRepository stuInfoRepository) {
        this.stuInfoRepository = stuInfoRepository;
    }

    @Override
    public Student findById(String id) {
        return studentRepository.findById(id).orElseThrow(RuntimeException::new);
    }

    @Override
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @Override
    public void deleteById(String id) {
        studentRepository.deleteById(id);
    }

    @Override
    public void save(Student student) {
        studentRepository.save(student);
    }

    @Override
    public Page<Student> findAll(Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page, pageSize);
        Page<Student> pageInfo = studentRepository.findAll(pageable);
        return pageInfo;
    }

    @Override
    public Page<StuInfo> findAll(Integer page, Integer pageSize, StuInfo stuInfo) {
        Pageable pageable = PageRequest.of(page, pageSize);
        Page<StuInfo> jzgPage = stuInfoRepository.findAll(new Specification<StuInfo>(){

            @Override
            public Predicate toPredicate(Root<StuInfo> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(null != stuInfo.getCollege_name() && stuInfo.getCollege_name() != ""){
                    list.add(criteriaBuilder.like(root.get("college_name").as(String.class), "%" + stuInfo.getCollege_name() + "%"));
                }
                if(null != stuInfo.getMajor_name() && stuInfo.getMajor_name() != ""){
                    list.add(criteriaBuilder.like(root.get("major_name").as(String.class), "%" + stuInfo.getMajor_name() + "%"));
                }
                if(null != stuInfo.getGrade_name() && stuInfo.getGrade_name() != ""){
                    list.add(criteriaBuilder.like(root.get("grade_name").as(String.class), "%" + stuInfo.getGrade_name() + "%"));
                }
                if(null != stuInfo.getSname() && stuInfo.getSname() != ""){
                    list.add(criteriaBuilder.like(root.get("sname").as(String.class), "%" + stuInfo.getSname() + "%"));
                }

                Predicate[] predicates = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(predicates));
            }
        }, pageable);

        return jzgPage;
    }


    @Override
    public List<StuInfo> getStuInfoByCollege(String name) {
        return stuInfoRepository.getStuInfoByCollege(name);
    }

    @Override
    public List<Map<Date, Integer>> getCount() {
        return studentRepository.getCount();
    }
}
